import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import '../../helper/uidata.dart';
import '../../model/dc_chartval.dart';
import '../../model/dc_ssdata.dart';
import '../../widgets/TimeLinePanel2.dart';
import '../../helper/routeHelper.dart';
import '../../model/daydata.dart';
import '../../model/scsq_q.dart';
import '../../model/scsq_sk.dart';
import '../../model/scsq_z.dart';
import '../../pages/sdzx/sdzx_helper.dart';
import '../../pages/sdzx/skdata.dart';
import '../../helper/Helper.dart';
import '../../helper/channelHelper.dart';
import '../../widgets/inputWidget.dart';

const ShowColor1 = Colors.blue;
const ShowColor2 = Colors.blue;

class DCSSDataPage extends StatefulWidget {
  @override
  DCSSDataPage({this.ifAppBar = false});
  final ifAppBar;
  @override
  DCSSDataPageState createState() => new DCSSDataPageState();
}

class DCSSDataPageState extends State<DCSSDataPage> {
  late DCSSData sk;
  late List<DCChartData> fdList;

  bool ifLoaded = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    LoadData();
  }

  void LoadData() async {
    sk = await DCSSData.Get() as DCSSData;
    UIData.SkData1 = sk;
    fdList = await DCChartData.GetList() as List<DCChartData>;

    setState(() {
      ifLoaded = true;
    });
    ChannelHelper.showToast("更新完成!");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: getFloatingButton(context),
      body: DCSSDataBody(context),
    );
  }

  Widget getFloatingButton(BuildContext context) {
    // ignore: unnecessary_new
    return new FloatingActionButton(
      child: Icon(Icons.refresh),
      onPressed: () {
        LoadData();
      },
    );
  }

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list2 = <Widget>[];
    List<Widget> list3 = <Widget>[];
    List<Widget> list4 = <Widget>[];
    List<Widget> list5 = <Widget>[];
    List<Widget> list6 = <Widget>[];

    list1.add(InputWidget.getGreyInfo("水库实时数据", Helper.DateTimeFormat(sk.TM!)));

    var v1 =
        InputWidget.getShowInfo("入库流量", sk.R.toStringAsFixed(0), ShowColor1);
    var v2 = InputWidget.getShowInfo(
        "出库流量", (sk.Qf + sk.Qp).toStringAsFixed(0), ShowColor1);
    var v3 =
        InputWidget.getShowInfo("发电流量", sk.Qf.toStringAsFixed(0), ShowColor1);
    var v4 =
        InputWidget.getShowInfo("泄洪流量", sk.Qp.toStringAsFixed(0), ShowColor1);
    var v5 =
        InputWidget.getShowInfo("水库水位", sk.H1.toStringAsFixed(2), ShowColor1);
    var v6 =
        InputWidget.getShowInfo("发电负荷", sk.Pz.toStringAsFixed(1), ShowColor1);
    var v7 =
        InputWidget.getShowInfo("1#机负荷", sk.P1.toStringAsFixed(1), ShowColor1);
    var v8 =
        InputWidget.getShowInfo("2#机负荷", sk.P2.toStringAsFixed(1), ShowColor1);
    var v9 =
        InputWidget.getShowInfo("3#机负荷", sk.P3.toStringAsFixed(1), ShowColor1);
    var v10 =
        InputWidget.getShowInfo("4#机负荷", sk.P4.toStringAsFixed(1), ShowColor1);
    var v11 =
        InputWidget.getShowInfo("下游水位", sk.H2.toStringAsFixed(2), ShowColor1);
    var v12 =
        InputWidget.getShowInfo("1#机流量", sk.Q1.toStringAsFixed(0), ShowColor1);
    var v13 =
        InputWidget.getShowInfo("2#机流量", sk.Q2.toStringAsFixed(0), ShowColor1);
    var v14 =
        InputWidget.getShowInfo("3#机流量", sk.Q3.toStringAsFixed(0), ShowColor1);
    var v15 =
        InputWidget.getShowInfo("4#机流量", sk.Q4.toStringAsFixed(0), ShowColor1);

    list4.add(InputWidget.getShowRow([v5, v1, v2], true));
    list4.add(InputWidget.getDivider());
    list4.add(InputWidget.getShowRow([v11, v6, v3], true));
    list1.add(InputWidget.inCard3(list4));

    list1.add(Container(height: 6.0));
    list1.add(InputWidget.getGreyInfo("机组负荷", Helper.DateTimeFormat2(sk.TM!)));
    list5.add(InputWidget.getShowRow([v7, v8, v9, v10], true));
    list1.add(InputWidget.inCard3(list5));

    list1.add(InputWidget.inCard3(list3));
    list1.add(Container(
      height: 36.0,
      child: Text(""),
    ));

    list1.add(InputWidget.inCard(getLinePanel()));

    list1.add(Container(
      height: 56.0,
      child: Text(""),
    ));

    return list1;
  }

  Widget getLinePanel() {
    var t1 = <String>[];
    var v1 = <double>[];
    var v2 = <double>[];
    var v3 = <double>[];

    for (var x1 in fdList) {
      t1.add(x1.TM);
      if (x1.FH > 0) {
        v1.add(x1.FH / 10);
      }

      v2.add(x1.JH / 10);
      v3.add(x1.N);
    }

    return TimeLinePanel2("发电负荷曲线", t1, [v1, v2, v3], 0, 80, 1, "万kW");
  }

  DCSSDataBody(BuildContext context) => new Scaffold(
      appBar: widget.ifAppBar
          ? AppBar(
              // title: bottomTabs[currentIndex].title,
              title: Text("发电实时数据"))
          : null,
      body: SingleChildScrollView(
        child: DCSSDataFields(context),
      ));

  DCSSDataFields(BuildContext context) {
    if (!ifLoaded)
      return Center(
          child: SpinKitCircle(
        color: Colors.blue,
      ));
    return Container(child: Column(children: getWidgetList(context)));
  }
}
